﻿using AutoMapper;
using ManagementApi.Models.System.Role;
using Modules.System.Dept;
using Modules.System.Role;
using Modules.System.User;
using PmSoft;
using PmSoft.Entity;
using static Modules.Project.ProjectConstants;

namespace ManagementApi.Models.System.User
{
    /// <summary>
    /// 用户视图对象
    /// </summary>
    public class UserDto
    {
        /// <summary>
		///  用户ID 
		///</summary>
        public int UserId { get; set; }

        /// <summary>
        /// 身份证号码
        ///</summary>
        public string? CardNo { get; set; }

        /// <summary>
        /// 姓名 
        ///</summary>
        public string? Name { get; set; }

        /// <summary>
        /// 性别
        ///</summary>
        public string? GenderName { get; set; }

        /// <summary>
        /// 手机号码 
        ///</summary>
        public string? Mobile { get; set; }

        /// <summary>
        /// 电子邮箱 
        ///</summary>
        public string? Email { get; set; }

        /// <summary>
        /// 归属主管单位名称
        ///</summary>
        public string? DeptName { get; set; }

        /// <summary>
        /// 所属角色名称(多个用,隔开)
        ///</summary>
        public string? RoleName { get; set; }

        /// <summary>
        /// 状态 正常，离岗,封禁 
        ///</summary>
        public string? StatusName { get; set; }

        /// <summary>
        /// 归属省名称
        ///</summary>
        public string? ProvinceName { get; set; }

        /// <summary>
        /// 归属地市名称
        ///</summary>
        public string? CityName { get; set; }

        /// <summary>
        /// 归属区县名称 
        ///</summary>
        public string? AreaName { get; set; }

        /// <summary>
        /// 归属省代码 
        ///</summary>
        public int? ProvinceCode { get; set; }
        /// <summary>
        /// 归属地市代码 
        ///</summary>
        public int? CityCode { get; set; }
        /// <summary>
        /// 归属区县代码 
        ///</summary>
        public int? AreaCode { get; set; }
        public 行业主管部门? IndustryType { get; set; }
        public string IndustryTypeName { get; set; }
        /// <summary>
        /// 创建时间 
        /// 默认值: CURRENT_TIMESTAMP
        ///</summary>
        public DateTime CreateTime { get; set; } = DateTime.Now;
    }
    public static partial class SysUserExceptions
    {
        public static UserDto AsUserDto(this SysUser user)
        {
            var mapper = ServiceAccessor.GetService<IMapper>();
            UserDto dto = new UserDto();
            mapper.Map(user, dto);
            dto.GenderName = user.Gender == 1 ? "男" : "女";
            dto.DeptName = user.GetDeptName();
            dto.RoleName = user.GetRoleName();
            dto.IndustryTypeName = user.IndustryType?.GetDescription()??"";
            if (user.Status == 1)
            {
                dto.StatusName = "正常";
            }
            else if (user.Status == 2)
            {
                dto.StatusName = "离岗";
            }
            else if (user.Status == 3)
            {
                dto.StatusName = "封禁";
            }
            return dto;

        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static string GetRoleName(this SysUser user)
        {
            var roleService = ServiceAccessor.GetService<RoleService>();
            var data = roleService.GetRolesByUserId(user.UserId);
            var roleNames = data.Select(a => a.Name);
            return string.Join(",", roleNames);
        }
        /// <summary>
        /// 获取用户角色Id集合
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static int[] GetRoleIds(this SysUser user)
        {
            var roleService = ServiceAccessor.GetService<RoleService>();
            var data = roleService.GetRolesByUserId(user.UserId);
            var roleIds = data.Select(a => a.RoleId);
            return roleIds.ToArray();
        }


        /// <summary>
        /// 获取主管单位名称
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static string GetDeptName(this SysUser user)
        {
            if (user.DeptId != null && user.DeptId != 0)
            {
                var deptService = ServiceAccessor.GetService<DeptService>();
                var data = deptService.GetSysDept((int)user.DeptId);
                if (data == null) return "";
                else return data.Name;
            }
            else return "";
        }
    }
}
